package com.cc.pms.service.impl;

import com.cc.common.core.domain.R;
import com.cc.common.utils.SecurityUtils;
import com.cc.pms.domain.UserChat;
import com.cc.pms.domain.vo.UserChatVo;
import com.cc.pms.mapper.UserChatMapper;
import com.cc.pms.service.IUserChatService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 存储用户之间的聊天记录 服务实现类
 * </p>
 *
 * @author corleone
 * @since 2025-04-19
 */
@Service
public class UserChatServiceImpl extends ServiceImpl<UserChatMapper, UserChat> implements IUserChatService {


    @Override
    public List<UserChatVo> queryMineChatList() {
        return baseMapper.queryMineChatList(SecurityUtils.getUserId());
    }

    @Override
    public List<UserChatVo> queryMineChatDetail(Long receiverId) {
        Long senderId = SecurityUtils.getUserId();
        return baseMapper.queryMineChatDetail(senderId , receiverId);
    }

    @Override
    public R<Long> sendChat(UserChat userChat) {
        if (userChat == null){
            return R.fail(-1 , "发送信息不能为空");
        }
        if (userChat.getReceiverId() == null || userChat.getContent() == null){
            return R.fail(-1 , "接收人员信息不能为空");
        }
        userChat.setSenderId(SecurityUtils.getUserId());
        return save(userChat) ? R.ok(userChat.getId()) : R.fail(-1 , "发送信息失败");
    }
}

